package com.example.myfoodrecipe.fragments.recipe.details

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.navArgs
import androidx.viewpager2.widget.ViewPager2
import com.example.myfoodrecipe.databinding.FragmentDetailBinding
import com.example.myfoodrecipe.fragments.recipe.adapter.ViewPagerAdapter
import com.example.myfoodrecipe.viewModel.FavViewModel

class DetailFragment : Fragment() {
    private lateinit var binding:FragmentDetailBinding
    private val recipeArgs: DetailFragmentArgs by navArgs()
    private val favViewModel:FavViewModel by viewModels()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentDetailBinding.inflate(inflater)
        binding.detailBtn.isSelected = true
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        init()
        //更新数据
        favViewModel.getAllFav()
        //观察FavFragment中的数据
        favViewModel.favorites.observe(viewLifecycleOwner){

            it.forEach { entity ->
                if (entity.recipe == recipeArgs.recipe){
                    binding.collectBtn.isSelected = true
                    return@forEach
                }
            }
        }
    }

    private fun init(){
        //接收dataBinding传递数据
        binding.recipe = recipeArgs.recipe
        binding.executePendingBindings()

        //实现detailFragment返回按键
        binding.backBtn.setOnClickListener {
            requireActivity().onBackPressed()
        }

        //实现detailBtn显示详情
        binding.detailBtn.setOnClickListener {
            selectDetail()
            binding.viewPager.currentItem = 0
        }

        //实现ingredientsBtn显示详情
        binding.ingredientsBtn.setOnClickListener {
            selectIngredient()
            binding.viewPager.currentItem = 1
        }

        //init ViewPager
        val fragments = listOf(SummaryFragment(recipeArgs.recipe.summary),IngredientFragment(recipeArgs.recipe.extendedIngredients))
        binding.viewPager.adapter = ViewPagerAdapter(fragments,requireActivity().supportFragmentManager,lifecycle)
            //滑动ViewPager时指标变化
        binding.viewPager.registerOnPageChangeCallback(object :ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                if (position == 0){
                    selectDetail()
                }else{
                    selectIngredient()
                }
            }
        })

        //fav_mark收藏按钮
        binding.collectBtn.setOnClickListener {
            if (binding.collectBtn.isSelected){
                //从数据库中删除这个食谱
                favViewModel.favorites.value?.forEach {
                    if (it.recipe == recipeArgs.recipe){
                        favViewModel.deleteFav(it)
                        binding.collectBtn.isSelected = false
                    }
                }

            }else{
                //将这个食谱插入到数据库中
                favViewModel.insertFav(recipeArgs.recipe)
                binding.collectBtn.isSelected = true
            }
        }
    }

    private fun selectDetail(){
            if (!binding.detailBtn.isSelected) {
                binding.detailBtn.isSelected = true
                binding.ingredientsBtn.isSelected = false
                val toDetail = binding.detailBtn.x - binding.ingredientsBtn.x
                indicatorTranslate(toDetail)
            }
    }

    private fun selectIngredient(){
            if (!binding.ingredientsBtn.isSelected) {
                binding.detailBtn.isSelected = false
                binding.ingredientsBtn.isSelected = true
                val toIngredients = binding.ingredientsBtn.x - binding.detailBtn.x
                indicatorTranslate(toIngredients)
            }
    }

    //指标移动
    private fun indicatorTranslate(value: Float){
        binding.indicatorView.animate()
                .translationXBy(value)
                .setDuration(300)
                .start()
    }
}